<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单分类" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入订单分类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单信息" prop="attribute">
        <el-input
          v-model="queryParams.attribute"
          placeholder="请输入订单信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格属性" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入规格属性"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="预约时间">
        <el-date-picker
          v-model="daterangeOrderTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      
      <el-form-item label="联系人" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入联系人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入联系人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
		      :loading="loading"
          @click="handleReceive"
        >抢单</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['business:order:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="expand" width="55" align="center" >
        <template slot-scope="scope">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-card class="box-card">
                <el-tabs >
                  <el-tab-pane label="服务地址">
                    <el-form  label-width="80px">
                      <el-row :gutter="20">
                        <el-col :span="12">
                          <el-form-item label="用户" prop="userName" style="margin-bottom: 8px;">
                            {{scope.row.orderAddress.userName}}
                          </el-form-item>
                        </el-col>
                        <el-col :span="12">
                          <el-form-item label="电话" prop="userTelephone" style="margin-bottom: 8px;">
                            {{scope.row.orderAddress.userTelephone}}
                          </el-form-item>
                        </el-col>
                      </el-row>
                      <el-row :gutter="20">
                        <el-col :span="24">
                          <el-form-item label="详细地址" prop="detail " style="margin-bottom: 8px;">
                            {{scope.row.orderAddress.detail}}
                          </el-form-item>
                        </el-col>
                      </el-row>
                    </el-form>
                  </el-tab-pane>
                </el-tabs>
              </el-card>
            </el-col>
            <el-col :span="16">
              <el-card class="box-card">
                <el-tabs >
                  <el-tab-pane label="支付记录">
                    <el-table v-loading="loading" :data="scope.row.paymentList">
                      <el-table-column label="支付金额" align="center" prop="amount" />
                      <el-table-column label="支付方式" align="center" prop="payWay" />
                      <el-table-column label="交易流水号" align="center" prop="transNo" :show-overflow-tooltip="true" />
                      <el-table-column label="类型" align="center" prop="payType" :formatter="formatPaytype" />
                      <el-table-column label="主题" align="center" prop="subject"  :show-overflow-tooltip="true"  />
                      <el-table-column label="内容" align="center" prop="body" />
                      <el-table-column label="支付状态" align="center" prop="status" >
                        <template slot-scope="scope">
                          <el-tag v-if="scope.row.status == 1" type="success" size="mini">已支付</el-tag>
                          <el-tag v-else type="danger" size="mini">未支付</el-tag>
                        </template>
                      </el-table-column>
                      <el-table-column label="支付时间" align="center" prop="successTime" width="180">
                        <template slot-scope="scope">
                          <span>{{ parseTime(scope.row.successTime) }}</span>
                        </template>
                      </el-table-column>
                      
                    </el-table>
                  </el-tab-pane>
                  <el-tab-pane label="抢单记录">
                    <el-table v-loading="loading" :data="scope.row.receiveList" >
                      <el-table-column label="抢单人" align="center" prop="userName" />
                      <el-table-column label="联系电话" align="center" prop="userTelephone" />
                      <el-table-column label="抢单时间" align="center" prop="createTime" width="180">
                        <template slot-scope="scope">
                          <span>{{ parseTime(scope.row.createTime) }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="备注" align="center" prop="remark" /> 
                      <el-table-column label="状态" align="center" prop="status" >
                        <template slot-scope="scope">
                          <el-tag v-if="scope.row.status == 1" type="success" size="mini">已确认</el-tag>
                          <el-tag v-else type="danger" size="mini">未确认</el-tag>
                        </template>
                      </el-table-column>                     
                    </el-table>
                  </el-tab-pane>
                  <el-tab-pane label="检查记录">
                    <el-table v-loading="loading" :data="scope.row.checkList" >
                      <el-table-column label="地址" align="center" prop="address" />                      
                      <el-table-column label="图片" align="center" prop="imgUrl" >
                        <template slot-scope="scope">
                          <el-image v-if="scope.row.imgUrl"
                            style="width: 100px; height: auto"
                            :src="scope.row.imgUrl" 
                            :preview-src-list="[scope.row.imgUrl]">
                          </el-image>
                        </template>
                      </el-table-column>
                      <el-table-column label="结论" align="center" prop="remark" />
                    </el-table>
                  </el-tab-pane>
                  <!-- <el-tab-pane label="服务记录">
                    <el-table v-loading="loading" :data="scope.row.serviceList" >
                      <el-table-column label="服务人员" align="center" prop="receiveName" />
                      <el-table-column label="服务开始时间" align="center" prop="beginTime" width="180">
                        <template slot-scope="scope">
                          <span>{{ parseTime(scope.row.beginTime) }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="服务结束时间" align="center" prop="endTime" width="180">
                        <template slot-scope="scope">
                          <span>{{ parseTime(scope.row.endTime) }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="图片" align="center" prop="imgUrl" >
                        <template slot-scope="scope">
                          <el-image v-if="scope.row.imgUrl"
                            style="width: 100px; height: auto"
                            :src="scope.row.imgUrl" 
                            :preview-src-list="[scope.row.imgUrl]">
                          </el-image>
                        </template>
                      </el-table-column>
                      <el-table-column label="备注" align="center" prop="remark" />
                    </el-table>
                  </el-tab-pane> -->
                </el-tabs>                
              </el-card>              
            </el-col>
          </el-row>          
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" width="170" prop="num" >
        <template slot-scope="scope">
          <el-button
              type="text"
              @click="handleShow(scope.row)"
            >{{scope.row.num}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="订单分类" align="center" prop="categoryName"  />
      <el-table-column label="订单信息" align="center" prop="attribute" show-overflow-tooltip />
      <el-table-column label="规格属性" align="center" prop="tags" show-overflow-tooltip />
      <el-table-column label="预约时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="username" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="区域" align="center" prop="district" width="170" />
      <el-table-column label="服务地址" align="center" prop="address" width="220" />
      <!-- <el-table-column label="接单人" align="center" prop="receiveName" /> -->
      <!-- <el-table-column label="接单电话" align="center" prop="receivePhone" /> -->
      <!-- <el-table-column label="接单限制" align="center" prop="receiveLimit" /> -->
      <!-- <el-table-column label="所属公司" align="center" prop="company" /> -->
      <!-- <el-table-column label="所属代理" align="center" prop="proxy" /> -->
      <!-- <el-table-column label="属性" align="center" prop="attribute" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"   width="80" /> -->
      <el-table-column label="备注" align="center" prop="detail" />
      <!-- <el-table-column label="下单时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
     -->
      <!-- <el-table-column label="应付" align="center" prop="payable" /> -->
      <!-- <el-table-column label="实付" align="center" prop="pay" /> -->
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div style="text-align:left;">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleShow(scope.row)"
            >详情</el-button>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-guide" 
              @click="handleDistr(scope.row)"
            >指派代理</el-button> -->
            <el-button
              size="mini"
              type="text" 
              icon="el-icon-search"
              @click="openDistributeReceive(scope.row)"
            >派单</el-button>         
           
          </div>
          
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

   

    <distribute-receive ref="distributeReceiveForm"  v-if="distributeOpen" @finishSelected="finishDistributeReceive"></distribute-receive>
    <show-order ref="showOrderForm" v-if="showOpen" ></show-order>
  </div>
</template>

<script>
import { listOrder, listToReceiveOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/business/order";
import DistributeReceive from '@/views/order/order/distributeReceive';
import ShowOrder from '@/views/order/order/showOrder';

export default {
  name: "Order",
  components: {
    DistributeReceive,
    ShowOrder,
  },
  data() {
 
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      orderTotal: 0,
      // 订单管理表格数据
      orderList: [],
      // 待抢单列表
      receiveOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      disabled: false,
      attrOpen: false,
      receiveOpen: false,
      addressOpen: false,
      distributeOpen: false,
      finishOpen: false,
      showOpen: false,
      receiveListOpen: false,
      startOpen: false,
      // 预约时间时间范围
      daterangeOrderTime: [],
      // 创建人名称时间范围
      daterangeCreateTime: [],
      // 状态数据字典
      statusOptions: [],
      payTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,
        receiveName: null,
        receivePhone: null,
        receiveLimit: null,
        group: null,
        attribute: null,
        status: null,
        createName: null,
        createId: null,
        createTime: null,
        payable: null,
        pay: null,
        payMethod: null
      },
      // 查询参数
      queryOrderParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,        
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,        
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attribute: [
          { required: true, message: "服务信息不能为空", trigger: "blur" }
        ],
        orderTime: [
          { required: true, message: "预约时间不能为空", trigger: "blur" }
        ],
        detail: [
          { required: true, message: "订单信息不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        receiveName: [
          { required: true, message: "接单人不能为空", trigger: "blur" }
        ],
        receivePhone: [
          { required: true, message: "接单电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tb_order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("tb_payment_type").then(response => {
      this.payTypeOptions = response.data;
    });
  },
  methods: {
    
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderTime && '' != this.daterangeOrderTime) {
        this.queryParams.params["beginOrderTime"] = this.daterangeOrderTime[0];
        this.queryParams.params["endOrderTime"] = this.daterangeOrderTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      this.queryParams.status = 18;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getReceiveOrderList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderTime && '' != this.daterangeOrderTime) {
        this.queryParams.params["beginOrderTime"] = this.daterangeOrderTime[0];
        this.queryParams.params["endOrderTime"] = this.daterangeOrderTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listToReceiveOrder(this.queryParams).then(response => {
        this.receiveOrderList = response.rows;
        this.orderTotal = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        num: null,
        detail: null,
        orderTime: null,
        userId: null,
        username: null,
        telephone: null,
        receiveId: null,
        receiveName: null,
        receivePhone: null,
        receiveLimit: null,
        group: null,
        attribute: null,
        status: 0,
        remark: null,
        createName: null,
        createId: null,
        createTime: null,
        payable: null,
        pay: null,
        payMethod: null,
        address: null,
        addressId: null,
        orderAddress: {
          address: null
        }
      };
      this.resetForm("form");
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 字典状态字典翻译
    formatPaytype(row, column) {
      
      return this.selectDictLabel(this.payTypeOptions, row.payType);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    openSelectAttribute(){
      this.attrOpen = true;
      this.$nextTick(()=>{
        this.$refs['selectAttrForm'].init();
      });
      
    },
    finishSelectAttr(data){
      this.form.attribute = data.name;
      this.form.pay = data.price;
      this.form.payable = data.price;
    },
    openSelectReceive(){
      this.receiveOpen = true;
      this.$nextTick(()=>{
        this.$refs['selectReceiveForm'].init();
      });
    },
    finishSelectReceive(data){
      console.log(data)
      this.form.receiveId = data.userId;
      this.form.receiveName = data.nickName;
      this.form.receivePhone = data.phonenumber;
      //
      this.form.proxyId = data.proxyId;
      this.form.proxy = data.proxy?.name;
      this.form.companyId = data.companyId;
      this.form.company = data.company?.name;
    },
    openSelectAddress(){
      this.addressOpen = true;
      this.$nextTick(()=>{
        this.$refs['selectAddressForm'].init();
      });
    },
    finishSelectAddress(data){
      console.log(data)
      this.form.orderAddress = data;
      this.form.address = data.detail;
      this.form.addressId = data.id;
      //
      this.form.userId = data.userId;
      this.form.username = data.userName;
      this.form.telephone = data.userTelephone;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.form.address = response.data.orderAddress.detail;
        this.open = true;
        this.title = "订单管理";
      });
    },
    handleShow(row){
      this.showOpen = true;
      this.$nextTick(() => {
        this.$refs['showOrderForm'].init(row);
      });
    },
   
    openDistributeReceive(row){
      this.distributeOpen = true;
      this.$nextTick(()=>{
        this.$refs['distributeReceiveForm'].init(row);
      });
    },
    finishDistributeReceive(data){
      this.getList();
    },
    startService(row){
      this.startOpen = true;
      this.$nextTick(()=>{
        this.$refs['startServiceForm'].init(row);
      });
    },
    startServiceSelected(row){
      this.getList();
    },
    finishOrder(row){
      this.finishOpen = true;
      this.$nextTick(()=>{
        this.$refs['finishOrderForm'].init(row);
      });
    },
    finishOrderSelected(data){
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.status = 10;//已接单
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    },
    handleReceive(){
      this.open = true;
      this.getReceiveOrderList();
    },
    handleReceiveOrder(row){
      this.$confirm('是否确认抢单信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          submitReceive({orderId: row.id}).then(response => {
            this.msgSuccess("抢单成功");
            this.open = false;
            this.getList();
          });
        })
      
    }
  }
};
</script>
